namespace Crux.Core.Dapper.Test;
public partial class UnitOfWorkTest
{
    [Fact]
    public void UpdateTest()
    {
        var model = repository.FirstOrDefault<TestModel>(c => c.Id > 0);

        Assert.NotNull(model);

        model.Password = $"{Random.Shared.Next(999999):000000}";

        unitOfWork.Update(model);
    }
    [Fact]
    public void UpdateWithTransactionRollbackTest()
    {
        using var transaction = unitOfWork.BeginTransaction();
        var model = repository.FirstOrDefault<TestModel>(c => c.Id > 0);

        Assert.NotNull(model);

        model.Password = $"{Random.Shared.Next(999999):000000}";

        unitOfWork.Update(model);

        transaction.Rollback();
    }
    [Fact]
    public void UpdateWithTransactionCommitTest()
    {
        using var transaction = unitOfWork.BeginTransaction();
        var model = repository.FirstOrDefault<TestModel>(c => c.Id > 0);

        Assert.NotNull(model);

        model.Password = $"{Random.Shared.Next(999999):000000}";

        unitOfWork.Update(model);

        transaction.Commit();
    }
    [Fact]
    public async Task UpdateAsyncTest()
    {
        var model = await repository.FirstOrDefaultAsync<TestModel>(c => c.Id > 0).ConfigureAwait(false);

        Assert.NotNull(model);

        model.Password = $"{Random.Shared.Next(999999):000000}";

        await unitOfWork.UpdateAsync(model).ConfigureAwait(false);
    }
    [Fact]
    public async Task UpdateAsyncWithTransactionRollbackTest()
    {
        using var transaction = unitOfWork.BeginTransaction();
        var model = await repository.FirstOrDefaultAsync<TestModel>(c => c.Id > 0).ConfigureAwait(false);

        Assert.NotNull(model);

        model.Password = $"{Random.Shared.Next(999999):000000}";

        await unitOfWork.UpdateAsync(model).ConfigureAwait(false);

        transaction.Rollback();
    }
    [Fact]
    public async Task UpdateAsyncWithTransactionCommitTest()
    {
        using var transaction = unitOfWork.BeginTransaction();
        var model = await repository.FirstOrDefaultAsync<TestModel>(c => c.Id > 0).ConfigureAwait(false);

        Assert.NotNull(model);

        model.Password = $"{Random.Shared.Next(999999):000000}";
        await unitOfWork.UpdateAsync(model).ConfigureAwait(false);

        transaction.Commit();
    }
    [Fact]
    public void UpdateWithPredicateTest()
    {
        var effectRow = unitOfWork.Update<TestModel>(c => c.Id == 0, p => new TestModel
        {
            Password = $"{Random.Shared.Next(999999):000000}"
        });

        Assert.Equal(0, effectRow);
    }
    [Fact]
    public void UpdateWithPredicateAndTransactionRollbackTest()
    {
        using var transaction = unitOfWork.BeginTransaction();

        var effectRow = unitOfWork.Update<TestModel>(c => c.Id == 0, p => new TestModel
        {
            Password = $"{Random.Shared.Next(999999):000000}"
        });

        Assert.Equal(0, effectRow);

        transaction.Rollback();
    }
    [Fact]
    public void UpdateWithPredicateAndTransactionCommitTest()
    {
        using var transaction = unitOfWork.BeginTransaction();

        var effectRow = unitOfWork.Update<TestModel>(c => c.Id == 0, p => new TestModel
        {
            Password = $"{Random.Shared.Next(999999):000000}"
        });

        Assert.Equal(0, effectRow);

        transaction.Commit();
    }
    [Fact]
    public async Task UpdateAsyncWithPredicateTest()
    {
        var effectRow = await unitOfWork.UpdateAsync<TestModel>(c => c.Id == 2, p => new TestModel
        {
            Password ="111",
        });

        Assert.Equal(0, effectRow);
    }
    [Fact]
    public async Task UpdateAsyncWithPredicateAndTransactionRollbackTest()
    {
        using var transaction = unitOfWork.BeginTransaction();
        var effectRow = await unitOfWork.UpdateAsync<TestModel>(c => c.Id == 0, p => new TestModel
        {
            Password = $"{Random.Shared.Next(999999):000000}"
        });

        Assert.Equal(0, effectRow);
        transaction.Rollback();
    }
    [Fact]
    public async Task UpdateAsyncWithPredicateAndTransactionCommitTest()
    {
        using var transaction = unitOfWork.BeginTransaction();
        var effectRow = await unitOfWork.UpdateAsync<TestModel>(c => c.Id == 0, p => new TestModel
        {
            Password = $"{Random.Shared.Next(999999):000000}"
        });

        Assert.Equal(0, effectRow);
        transaction.Commit();
    }
    [Fact]
    public void UpdateRangeTest()
    {
        var models = repository.Query<TestModel>(p => p.Id > 0).ToList();

        models.ForEach(c => c.Password = $"{Random.Shared.Next(999999999):000000000}");

        var effectRow = unitOfWork.UpdateRange(models);

        Assert.True(effectRow > 0);
    }
    [Fact]
    public void UpdateRangeWithTransactionRollbackTest()
    {
        using var transaction = unitOfWork.BeginTransaction();
        var models = repository.Query<TestModel>(p => p.Id > 0).ToList();

        models.ForEach(c => c.Password = $"{Random.Shared.Next(999999999):000000000}");

        var effectRow = unitOfWork.UpdateRange(models);

        Assert.True(effectRow > 0);
        transaction.Rollback();
    }
    [Fact]
    public void UpdateRangeWithTransactionCommitTest()
    {
        using var transaction = unitOfWork.BeginTransaction();
        var models = repository.Query<TestModel>(p => p.Id > 0).ToList();

        models.ForEach(c => c.Password = $"{Random.Shared.Next(999999999):000000000}");

        var effectRow = unitOfWork.UpdateRange(models);

        Assert.True(effectRow > 0);
        transaction.Commit();
    }
    [Fact]
    public async Task UpdateRangeAsyncTest()
    {
        var models = (await repository.QueryAsync<TestModel>(p => p.Id > 0)).ToList();

        models.ForEach(c => c.Password = $"{Random.Shared.Next(999999999):000000000}");

        var effectRow = await unitOfWork.UpdateRangeAsync(models).ConfigureAwait(false);

        Assert.True(effectRow > 0);
    }
    [Fact]
    public async Task UpdateRangeAsyncWithTransactionRollbackTest()
    {
        using var transaction = unitOfWork.BeginTransaction();
        var models = (await repository.QueryAsync<TestModel>(p => p.Id > 0)).ToList();

        var effectRow = await unitOfWork.UpdateRangeAsync(models).ConfigureAwait(false);

        Assert.True(effectRow > 0);
        transaction.Rollback();
    }
    [Fact]
    public async Task UpdateRangeAsyncWithTransactionCommitTest()
    {
        using var transaction = unitOfWork.BeginTransaction();
        var models = (await repository.QueryAsync<TestModel>(p => p.Id > 0)).ToList();

        var effectRow = await unitOfWork.UpdateRangeAsync(models).ConfigureAwait(false);

        Assert.True(effectRow > 0);
        transaction.Commit();
    }
}